<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_inventori_turn_over extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
	//	$this->load->model('isp_inventori_to_1_model', 'ISPKontrak');
		$this->load->model('isp_inventori_to_2_model', 'IspInventoriTO');
		$this->load->model('combo_model', 'Combo');
		
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		// Display Index
		$data['title'] = "Inventori Peminjaman";
		$query = $this->db->query("SELECT id_peminjaman FROM (SELECT * FROM isp_inventori_peminjaman ORDER BY id_peminjaman DESC) WHERE rownum <=1");
				 				if ($query->row_array()) {
				 					$getquery = $query->row_array();
				 					$rowcount = $getquery['id_peminjaman'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['id_peminjaman_auto'] = $autonum;
		viewByTemplate('isp_inventori_turn_over/index', $data, 'grid');
	}
	
	// function getdata()
	// {
	// 	// Ajax Response For Datagrid
	// 	echo $this->ISPKontrak->GetJSON();
	// }
	
	function getdata()
	{
		// Ajax Response For Datagrid
		$this->secure_session();
		echo $this->IspInventoriTO->GetJSON();
	}	

	function getdata2()
	{
		// Ajax Response For Datagrid
		$this->secure_session();
		echo $this->IspInventoriTO->GetJSON2();
	}

	function getdata3()
	{
		// Ajax Response For Datagrid
		$this->secure_session();
		echo $this->IspInventoriTO->GetJSON3();
	}	
	
	function id_auto(){
		$query = $this->db->query("SELECT id_peminjaman FROM (SELECT * FROM isp_inventori_peminjaman ORDER BY id_peminjaman DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_peminjaman'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_peminjaman_auto'] = $autonum;
		echo json_encode($data);
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_barang") == '') {
			$error->Add("kode_barang", "Kode Barang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_inventori_peminjaman WHERE kode_kontrak = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_kontrak"))))."'")) > 0) {
			$error->Add("kode_barang", "Kode Barang", $tpl_duplicate);
		} else if (post("jumlah") == '') {
			$error->Add("jumlah", "Jumlah Pinjaman", $tpl_empty);
		} else if (post("tgl_dipinjam") == '') {
			$error->Add("tgl_dipinjam", "Tanggal Dipinjam", $tpl_empty);
		} else if (post("banyak") == '') {
			$error->Add("banyak", "Nama Barang", $tpl_empty);
		} else if (post("kode_kontrak") == '') {
			$error->Add("kode_kontrak", "Kode Kontrak ", $tpl_empty);
		} else if (post("status") == '') {
			$error->Add("status", "Status ", $tpl_empty);
		}
 

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'id_peminjaman' => $this->input->post('id_peminjaman'),
					'id_inventori' => $this->input->post('id_inventori'),
					'id_kontrak' => $this->input->post('id_kontrak'),
					'kode_barang' => $this->input->post('kode_barang'),
					'nama_barang' => $this->input->post('nama_barang'),
					'banyak' => $this->input->post('update_stock'),
					'jumlah' => $this->input->post('jumlah'),
					'tgl_dipinjam' => $this->input->post('tgl_dipinjam'),
					'kode_kontrak' => $this->input->post('kode_kontrak'),
					'kode_customer' => $this->input->post('kode_customer'),
					'nama_customer' => $this->input->post('nama_customer'),
					'status' => $this->input->post('status'),
					'keterangan' => $this->input->post('keterangan'),
					);

				$data['tgl_dipinjam']	= ($data['tgl_dipinjam'] == '' ? NULL : date_format3($data['tgl_dipinjam'], 'YYYY-MM-DD'));
				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspInventoriTO->Add($data);

					$cek_data = $this->db->query("UPDATE isp_inventori SET banyak='".$this->input->post('update_stock')."' WHERE kode_inventori ='".$this->input->post('kode_barang')."' ");
					$cek_data = $this->db->query("UPDATE isp_inventori_peminjaman SET banyak='".$this->input->post('update_stock')."' WHERE kode_barang ='".$this->input->post('kode_barang')."' ");
					//echo $this->db->last_query();
					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspInventoriTO->Update($data, 
										array('id_peminjaman'	=> $data['id_peminjaman']));

					$cek_data = $this->db->query("UPDATE isp_inventori SET banyak='".$this->input->post('update_stock')."' WHERE kode_inventori ='".$this->input->post('kode_barang')."' ");
					$cek_data = $this->db->query("UPDATE isp_inventori_peminjaman SET banyak='".$this->input->post('update_stock')."' WHERE kode_barang ='".$this->input->post('kode_barang')."' ");
					//echo $this->db->last_query();

					$ls_response = "Data Tersimpan [Edited]";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "Data Terhapus";
		$id_peminjaman 	= get('id_peminjaman');
		
		if ($id_peminjaman != "")
		{
			// Delete Single
			$arr_rule	= array('id_peminjaman' => $id_peminjaman);
			$ls_result = $this->IspInventoriTO->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_peminjaman' => $row['id_peminjaman']);
				$ls_result .= $this->IspInventoriTO->DeleteSingle($arr_rule);
			}
		}
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}	
	}
?>